package com.ec50n9.server.config

import com.ec50n9.server.module.issue.Issue
import com.ec50n9.server.module.issue.IssueRepository
import com.ec50n9.server.module.issueCategory.IssueCategory
import com.ec50n9.server.module.issueCategory.IssueCategoryRepository
import com.ec50n9.server.module.resource.Resource
import com.ec50n9.server.module.resource.ResourceRepository
import com.ec50n9.server.module.role.Role
import com.ec50n9.server.module.role.RoleRepository
import com.ec50n9.server.module.user.User
import com.ec50n9.server.module.user.UserRepository
import org.springframework.boot.ApplicationRunner
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class DatabaseConfig {

    @Bean
    fun databaseInitializer(
        userRepository: UserRepository,
        roleRepository: RoleRepository,
        resourceRepository: ResourceRepository,
        issueCategoryRepository: IssueCategoryRepository,
        issueRepository: IssueRepository
    ) = ApplicationRunner {
        val user_shello = userRepository.save(
            User(
                "shello",
                "123456",
                "ec50n9",
                avatar = "/vite.svg"
            )
        )

        val role_admin = roleRepository.save(Role("管理员", "admin"))
        val role_user = roleRepository.save(Role("普通用户", "user"))

        val res_get_users = resourceRepository.save(Resource("获取用户列表", "get_users", "GET", "/api/users"))
        val res_get_user = resourceRepository.save(Resource("获取角色列表", "get_roles", "GET", "/api/roles"))
        val res_post_users = resourceRepository.save(Resource("获取用户列表", "post_users", "POST", "/api/users"))
        val res_patch_users = resourceRepository.save(Resource("获取用户列表", "patch_users", "PATCH", "/api/users/*"))
        val res_delete_users =
            resourceRepository.save(Resource("获取用户列表", "delete_users", "DELETE", "/api/users/*"))
        val res_god = resourceRepository.save(Resource("神", "god", "*", "*", 1))

        role_admin.resources.addAll(listOf(res_god))
        roleRepository.save(role_admin)

        user_shello.roles.addAll(listOf(role_admin, role_user))
        userRepository.save(user_shello)

        val softwareCategory = issueCategoryRepository.save(IssueCategory("软件", "software"))
        val hardwareCategory = issueCategoryRepository.save(IssueCategory("硬件", "hardware"))
        issueRepository.save(
            Issue(
                title = "office 无法正常使用",
                content = "打开就闪退",
                category = softwareCategory,
                author = user_shello
            )
        )
        issueRepository.save(
            Issue(
                title = "电脑开不了机",
                content = "不知道为什么就是突然开不了机了",
                category = hardwareCategory,
                author = user_shello
            )
        )
    }
}
